package v1

import (
	"fmt"
	//"github.com/EDDYCJY/go-gin-example/service/tag_service"

	"gitee.com/xuaxu/go-gin/pkg/app"
	"gitee.com/xuaxu/go-gin/pkg/e"
	"gitee.com/xuaxu/go-gin/pkg/util"
	"gitee.com/xuaxu/go-gin/service/article"
	//"gitee.com/xuaxu/go-gin/service/article_service"
	"github.com/astaxie/beego/validation"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
	"net/http"
)

type Data struct {
	List  []article_service.ArticleNew
	Count int
}

// @Summary Get a single article
// @Produce  json
// @Param id path int true "ID"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles/{id} [get]
func GetArticle(c *gin.Context) {
	appG := app.Gin{C: c}
	id := com.StrTo(c.PostForm("id")).MustInt()
	page := com.StrTo(c.PostForm("page")).MustInt()
	size := com.StrTo(c.PostForm("size")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 0, "id")

	if valid.HasErrors() {
		app.MarkErrors(valid.Errors)
		appG.Response(http.StatusBadRequest, e.INVALID_PARAMS, nil)
		return
	}
	token := c.GetHeader("token")
	Claims, err := util.ParseToken(token)
	fmt.Println(Claims.StandardClaims.Id, 1, Claims.Jti, 3, Claims.StandardClaims.Issuer, 2)
	//
	articleService := article_service.Article{}
	//exists, err := articleService.ExistByID()
	//if err != nil {
	//	appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
	//	return
	//}
	//if !exists {
	//	appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_ARTICLE, nil)
	//	return
	//}

	article, err, count := articleService.Get(page, size)
	if err != nil {
		appG.Response(http.StatusInternalServerError, e.ERROR_GET_ARTICLE_FAIL, nil)
		return
	}
	var list Data
	list.List = article
	list.Count = count
	appG.Response(http.StatusOK, e.SUCCESS, list)
}

// @Summary Get multiple articles
// @Produce  json
// @Param tag_id body int false "TagID"
// @Param state body int false "State"
// @Param created_by body int false "CreatedBy"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles [get]

type AddArticleForm struct {
	TagID         int    `form:"tag_id" valid:"Required;Min(1)"`
	Title         string `form:"title" valid:"Required;MaxSize(100)"`
	Desc          string `form:"desc" valid:"Required;MaxSize(255)"`
	Content       string `form:"content" valid:"Required;MaxSize(65535)"`
	CreatedBy     string `form:"created_by" valid:"Required;MaxSize(100)"`
	CoverImageUrl string `form:"cover_image_url" valid:"Required;MaxSize(255)"`
	State         int    `form:"state" valid:"Range(0,1)"`
}

// @Summary Add article
// @Produce  json
// @Param tag_id body int true "TagID"
// @Param title body string true "Title"
// @Param desc body string true "Desc"
// @Param content body string true "Content"
// @Param created_by body string true "CreatedBy"
// @Param state body int true "State"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles [post]
//func AddArticle(c *gin.Context) {
//	var (
//		appG = app.Gin{C: c}
//		form AddArticleForm
//	)
//
//	httpCode, errCode := app.BindAndValid(c, &form)
//	if errCode != e.SUCCESS {
//		appG.Response(httpCode, errCode, nil)
//		return
//	}
//
//	tagService := tag_service.Tag{ID: form.TagID}
//	exists, err := tagService.ExistByID()
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
//		return
//	}
//
//	if !exists {
//		appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_TAG, nil)
//		return
//	}
//
//	articleService := article_service.Article{
//		TagID:         form.TagID,
//		Title:         form.Title,
//		Desc:          form.Desc,
//		Content:       form.Content,
//		CoverImageUrl: form.CoverImageUrl,
//		State:         form.State,
//		CreatedBy:     form.CreatedBy,
//	}
//	if err := articleService.Add(); err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_ADD_ARTICLE_FAIL, nil)
//		return
//	}
//
//	appG.Response(http.StatusOK, e.SUCCESS, nil)
//}

type EditArticleForm struct {
	ID            int    `form:"id" valid:"Required;Min(1)"`
	TagID         int    `form:"tag_id" valid:"Required;Min(1)"`
	Title         string `form:"title" valid:"Required;MaxSize(100)"`
	Desc          string `form:"desc" valid:"Required;MaxSize(255)"`
	Content       string `form:"content" valid:"Required;MaxSize(65535)"`
	ModifiedBy    string `form:"modified_by" valid:"Required;MaxSize(100)"`
	CoverImageUrl string `form:"cover_image_url" valid:"Required;MaxSize(255)"`
	State         int    `form:"state" valid:"Range(0,1)"`
}

// @Summary Update article
// @Produce  json
// @Param id path int true "ID"
// @Param tag_id body string false "TagID"
// @Param title body string false "Title"
// @Param desc body string false "Desc"
// @Param content body string false "Content"
// @Param modified_by body string true "ModifiedBy"
// @Param state body int false "State"
// @Success 200 {object} app.Response
// @Failure 500 {object} app.Response
// @Router /api/v1/articles/{id} [put]
//func EditArticle(c *gin.Context) {
//	var (
//		appG = app.Gin{C: c}
//		form = EditArticleForm{ID: com.StrTo(c.Param("id")).MustInt()}
//	)
//
//	httpCode, errCode := app.BindAndValid(c, &form)
//	if errCode != e.SUCCESS {
//		appG.Response(httpCode, errCode, nil)
//		return
//	}
//
//	articleService := article_service.Article{
//		ID:            form.ID,
//		TagID:         form.TagID,
//		Title:         form.Title,
//		Desc:          form.Desc,
//		Content:       form.Content,
//		CoverImageUrl: form.CoverImageUrl,
//		ModifiedBy:    form.ModifiedBy,
//		State:         form.State,
//	}
//	exists, err := articleService.ExistByID()
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
//		return
//	}
//	if !exists {
//		appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_ARTICLE, nil)
//		return
//	}
//
//	tagService := tag_service.Tag{ID: form.TagID}
//	exists, err = tagService.ExistByID()
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_EXIST_TAG_FAIL, nil)
//		return
//	}
//
//	if !exists {
//		appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_TAG, nil)
//		return
//	}
//
//	err = articleService.Edit()
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_EDIT_ARTICLE_FAIL, nil)
//		return
//	}
//
//	appG.Response(http.StatusOK, e.SUCCESS, nil)
//}
//
//// @Summary Delete article
//// @Produce  json
//// @Param id path int true "ID"
//// @Success 200 {object} app.Response
//// @Failure 500 {object} app.Response
//// @Router /api/v1/articles/{id} [delete]
//func DeleteArticle(c *gin.Context) {
//	appG := app.Gin{C: c}
//	valid := validation.Validation{}
//	id := com.StrTo(c.Param("id")).MustInt()
//	valid.Min(id, 1, "id").Message("ID必须大于0")
//
//	if valid.HasErrors() {
//		app.MarkErrors(valid.Errors)
//		appG.Response(http.StatusOK, e.INVALID_PARAMS, nil)
//		return
//	}
//
//	articleService := article_service.Article{ID: id}
//	exists, err := articleService.ExistByID()
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_CHECK_EXIST_ARTICLE_FAIL, nil)
//		return
//	}
//	if !exists {
//		appG.Response(http.StatusOK, e.ERROR_NOT_EXIST_ARTICLE, nil)
//		return
//	}
//
//	err = articleService.Delete()
//	if err != nil {
//		appG.Response(http.StatusInternalServerError, e.ERROR_DELETE_ARTICLE_FAIL, nil)
//		return
//	}
//
//	appG.Response(http.StatusOK, e.SUCCESS, nil)
//}
//
//const (
//	QRCODE_URL = "https://github.com/EDDYCJY/blog#gin%E7%B3%BB%E5%88%97%E7%9B%AE%E5%BD%95"
//)

func GenerateArticlePoster(c *gin.Context) {
	//appG := app.Gin{C: c}
	//article := &article_service.Article{}
	//qr := qrcode.NewQrCode(QRCODE_URL, 300, 300, qr.M, qr.Auto)
	//posterName := article_service.GetPosterFlag() + "-" + qrcode.GetQrCodeFileName(qr.URL) + qr.GetQrCodeExt()
	//articlePoster := article_service.NewArticlePoster(posterName, article, qr)
	//articlePosterBgService := article_service.NewArticlePosterBg(
	//	"bg.jpg",
	//	articlePoster,
	//	&article_service.Rect{
	//		X0: 0,
	//		Y0: 0,
	//		X1: 550,
	//		Y1: 700,
	//	},
	//	&article_service.Pt{
	//		X: 125,
	//		Y: 298,
	//	},
	//)
	//
	//_, filePath, err := articlePosterBgService.Generate()
	//if err != nil {
	//	appG.Response(http.StatusInternalServerError, e.ERROR_GEN_ARTICLE_POSTER_FAIL, nil)
	//	return
	//}
	//
	//appG.Response(http.StatusOK, e.SUCCESS, map[string]string{
	//	"poster_url":      qrcode.GetQrCodeFullUrl(posterName),
	//	"poster_save_url": filePath + posterName,
	//})
}
